# 导入
import re
import time

import requests
from wxauto import WeChat

def http_req(mf):
    question = mf.content
    print(mf.content)
    res = requests.get("http://localhost:8080/example", params={"message": "朋友说:"+question+" 我该怎么微信用文言文回复他毒舌怼他攻击他戏谑他，请直接给出你的回复，只要文言文回复内容"})
    res_list = re.split(r'[,\s;|。，！？]+', res.text.replace("\"",""))
    ress = ""
    for i,s in enumerate(res_list):
        ress += s+","
        if i > 5 or len(ress)>60:
            break
    mf.quote(""+ress)

def http_req1(mf):
    question = mf.content
    print(mf.content)
    res = requests.get("http://localhost:8080/example", params={"message": "朋友说:"+question+" 我该怎么微信用文言文回复他毒舌怼他攻击他戏谑他，请直接给出你的回复，只要文言文回复内容"})
    res = res.text
    print(res)
    index = []
    for i,r in enumerate(res):
        if r == "\"" or r == "“" or r== "”":
            index.append(int(i))
    print(index[0]+1,index[-1])
    ress = res[index[-2]:index[-1]-1]

    mf.quote(""+ress)

def auto_reply(mf):
    local_time = time.localtime()
    formatted_local_time = time.strftime('%Y-%m-%d %H:%M:%S', local_time)
    mf.quote(f"[自动回复]本人正忙,稍后回复您\n({formatted_local_time})")
# 获取微信窗口对象
wx = WeChat()
# 输出 > 初始化成功，获取到已登录窗口：xxxx
listen_list = [
"特斯拉",
    "山下風",

]
for i in listen_list:
    wx.AddListenChat(who=i, savepic=False)
# 持续监听消息，并且收到消息后回复“收到”
wait = 1  # 设置1秒查看一次是否有新消息
while True:
    msgs = wx.GetListenMessage()
    for chat in msgs:
        who = chat.who  # 获取聊天窗口名（人或群名）
        one_msgs = msgs.get(chat)  # 获取消息内容
        for msg in one_msgs:
            msgtype = msg.type  # 获取消息类型
            content = msg.content  # 获取消息内容，字符串类型的消息内容
            print(f"{msgtype}：{content}")
        msg_friend = filter(lambda m:m.type in ["friend"],one_msgs)
        msg_friend = list(msg_friend)
        if len(msg_friend) > 0:
                for mf in msg_friend:
                    http_req1(mf)
        print(list(msg_friend))


    time.sleep(wait)
